package com.sgb.kjwl.view.ui.activity.self.packers;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.bumptech.glide.Glide;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivitySelfTeamWorkerBinding;
import com.sgb.kjwl.di.self.DaggerSelfComponent;
import com.sgb.kjwl.di.self.SelfModule;
import com.sgb.kjwl.model.entity.self.SelfTeamWorkerEntity;
import com.sgb.kjwl.view.ui.adapter.self.packers.SelfTeamWorkerAdapter;
import com.sgb.kjwl.viewmodel.self.packers.SelfTeamWorkerModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.dialog.CommonDialog;
import com.swgk.core.helper.OnMultiClickListener;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.List;
import java.util.Objects;

import javax.inject.Inject;

/**
 * description:个人中心-团队工人发布
 * @author 毁灭霸者
 * @date 2020/8/13
 */
public class SelfTeamWorkerActivity extends AppActivity {
    public static final int REQUEST_CODE = 101;
    public static final int REQUEST_ADD_CODE = 102;
    @Inject
    SelfTeamWorkerModel viewModel;
    private ActivitySelfTeamWorkerBinding binding;
    private SelfTeamWorkerAdapter selfTeamWorkerAdapter;

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerSelfComponent.builder().appComponent(appComponent).selfModule(new SelfModule(this)).build().inject(this);
    }

    @Override
    protected boolean statusBarDark() {
        return false;
    }

    public static void start(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, SelfTeamWorkerActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActionBar.setMenuFunctionClick(new OnMultiClickListener() {
            @Override
            public void onMultiClick(View v) {
                SelfTeamWorkerEditActivity.start(SelfTeamWorkerActivity.this, REQUEST_ADD_CODE);
            }
        });
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_self_team_worker);
        binding.setViewModel(viewModel);
        if (selfTeamWorkerAdapter == null) {
            selfTeamWorkerAdapter = new SelfTeamWorkerAdapter(getActivity(), null, Glide.with(this));
        }
        selfTeamWorkerAdapter.setItemCallBack(new SelfTeamWorkerAdapter.ItemCallBack() {
            @Override
            public void del(String id) {
                optState(id);
            }

            @Override
            public void edit(String id) {
                SelfTeamWorkerEditActivity.start(SelfTeamWorkerActivity.this, id, REQUEST_ADD_CODE);
            }
        });
        selfTeamWorkerAdapter.setOnItemClick(new AdapterOnItemClick<SelfTeamWorkerEntity>() {
            @Override
            public void onItemClick(SelfTeamWorkerEntity selfTeamWorkerEntity, int position) {
                viewModel.getH5Url(selfTeamWorkerEntity.getNo(), selfTeamWorkerEntity.getNo());
            }
        });
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        DividerItemDecoration divider = new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL);
        divider.setDrawable(Objects.requireNonNull(ContextCompat.getDrawable(getActivity(), R.drawable.shape_list_divide)));
        binding.recyclerView.addItemDecoration(divider);
        binding.recyclerView.setAdapter(selfTeamWorkerAdapter);

        binding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                viewModel.getLegalListModel(false);
            }

            @Override
            public void onLoadMore() {
                viewModel.getLegalListModel(true);
            }
        });
    }

    @Override
    protected void init() {
        viewModel.getLegalListModel(false);
    }

    public void showList(List<SelfTeamWorkerEntity> list, boolean isLoadMore, boolean isLast) {
        if (isLoadMore) {
            binding.recyclerView.loadMoreComplete();
            if (list != null && list.size() > 0) {
                selfTeamWorkerAdapter.addDatas(list);
            }
            binding.recyclerView.setNoMore(isLast);
        } else {
            binding.recyclerView.refreshComplete();
            if (list != null && list.size() > 0) {
                binding.emptyView.setVisibility(View.GONE);
                selfTeamWorkerAdapter.setDatas(list);
            } else {
                selfTeamWorkerAdapter.clearDatas();
                binding.emptyView.setVisibility(View.VISIBLE);
            }
        }
    }

    private void optState(String id) {
        new CommonDialog(getActivity()).builder().setPositiveButton("确定").setNegativeButton("取消")
                .setTitle("确认删除？")
                .setDialogCallBack(new CommonDialog.DialogCallBack() {
                    @Override
                    public void onRightClick() {
                        viewModel.delList(id);
                    }

                    @Override
                    public void onLeftClick() {

                    }
                }).show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        if (resultCode == RESULT_OK) {
            if (requestCode == REQUEST_CODE || requestCode == REQUEST_ADD_CODE) {
                viewModel.getLegalListModel(false);
            }
        }

    }
}
